package com.rainng.coursesystem.module.task.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rainng.coursesystem.controller.admin.vo.VisBatchEntityVo;
import com.rainng.coursesystem.model.entity.VisBatchEntity;
import com.rainng.coursesystem.module.system.service.SysUserService;
import com.rainng.coursesystem.module.task.mapper.BatchMapper;
import com.rainng.coursesystem.module.task.service.BatchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class BatchServiceImpl extends ServiceImpl<BatchMapper, VisBatchEntity> implements BatchService {

    @Autowired
    private SysUserService sysUserService;

    @Override
    public List<VisBatchEntity> getBatchList(VisBatchEntityVo reqDTO) {

        Page<VisBatchEntity> pageParam = new Page<>(reqDTO.getPageNum(), reqDTO.getPageSize());
        IPage<VisBatchEntity> entities = baseMapper.queryGroupPageList(reqDTO,pageParam);
        List<VisBatchEntity> res = entities.getRecords();
        return res;
    }

    @Override
    public Integer getBatchCount(VisBatchEntityVo reqDTO) {
        return baseMapper.queryGroupCount(reqDTO);
    }

    @Override
    public String getThisBatchName() {
        List<VisBatchEntity> res = baseMapper.getThisBatchInfo(new Date());
        if(res.size() == 0){
            return "";
        }
        return res.get(0).getName();
    }

    @Override
    public VisBatchEntity insertBatchData() {
        VisBatchEntity batch=baseMapper.getMaxBatchInfo();
        Date endTime=batch.getEndTime();
        long diff=new Date().getTime() - endTime.getTime();
        if (diff>0){
            Integer num= Integer.parseInt(batch.getName().trim()) + 1;
            batch.setName(num.toString());
            Date now=new Date();
            batch.setCreateTime(now);
            batch.setStartTime(new Date(batch.getEndTime().getTime() + 1000));
            LocalDate now1 = LocalDate.now();
            LocalDateTime nextMonthSixthMorning = now1.with(TemporalAdjusters.firstDayOfNextMonth())
                    .withDayOfMonth(6)
                    .atStartOfDay();
            Date end_time=Date.from(nextMonthSixthMorning.atZone(ZoneId.systemDefault()).toInstant());
            batch.setEndTime(end_time);
            this.save(batch);

        }
        return null;
    }

    @Override
    public List<VisBatchEntity> getThisBatchInfo() {
        List<VisBatchEntity> res = baseMapper.getThisBatchInfo(new Date());
        if(res.size() == 0){
            return null;
        }
        return res;
    }


//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void save(TaskDetailDTO reqDto) {
//        Task task = new Task();
//        BeanMapper.copy(reqDto, task);
//        ResultVO<Map<String, String>> vo = userService.getLoginStatusWithDepartment();
//        String username = vo.getData().get("username");
//        if (task.getId() == null || task.getId() == "") {
//            if (task.getApplyNum() != null) {
//                List<Task> list = baseMapper.selectList(new LambdaQueryWrapper<Task>().eq(Task::getApplyNum, reqDto.getApplyNum()));
//                if (list.size() > 0) {
//                    throw new ServiceException("新增任务失败，申请编号重复！");
//                }
//            }
//            task.setId(IdWorker.getIdStr());
//            task.setCreateBy(username);
//            task.setCreateTime(new Date());
//            this.save(task);
//        } else {
//            task.setCreateBy(username);
//            this.updateById(task);
//        }
//    }


}
